<?php
/**
 * 共用请求参数验证器
 * 发仔<i@fazi.me> 2018-07-09
 */

namespace app\open\validate;

use app\base\validate\BaseValidate;
class CommonValidate extends BaseValidate
{
    //验证
    protected $rule = [
        'page' => 'isPositiveInt',
        'pagenum' => 'isPositiveInt',
        'where_json' => 'isWhereJson',
        'fields' => 'checkField',
        'ids' => 'require',
        'is_all' => 'integer',
        'is_tree' => 'integer',
        'with_trashed' => 'integer',
        'sort' => 'string',
    ];

    // 默认值
    protected $default = [
        'page' => 1,
        'pagenum' => 20,
        'where_json' => 'isWhereJson',
        'fields' => '*',
        'is_all' => 0,
        'is_tree' => 0,
        'with_trashed' => 0,
    ];

    protected $message = [
        'page.isPositiveInt' => '页数只能为正整数',
        'pagenum.isPositiveInt' => '每页数量只能为正整数',
        'fields.isNotEmpty' => '字段不能为空',
        'where_json.isWhereJson' => '不是合法的json字符串',
    ];

    protected $scene = [
        'list' => ['page' ,'pagenum',  'fields', 'where_json','is_all','is_tree','with_trashed','sort'],
        'delete' => ['ids'],
    ];

    public function checkField($value){
        if($value=='*') return true;
        return true;
    }
}